package com.huika.renrench.control.my.merchant;

import java.util.ArrayList;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.my.adapter.MyProClassifyAdapter;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.ProTypeBean;
import com.huika.renrench.view.TitleBarHelper;

public class MyProgramClassifyAct extends BaseAct implements OnItemClickListener {
	private ListView lv;
	private MyProClassifyAdapter adapter;
	private ArrayList<ProTypeBean> list = new ArrayList<ProTypeBean>();
	private String[] types = { "美食", "家具维修", "手机维修", "开锁服务", "家具木匠", "家电维修", "其它" };

	@Override
	protected void initData() {
//		for (int i = 0; i < types.length; i++) {
//			list.add(types[i]);
//		}
	}

	@Override
	public void setRootView() {
		setContentView(R.layout.activity_mypro_classify);
	}

	@Override
	protected void initWidget() {
		TitleBarHelper titleBarHelper = new TitleBarHelper(this, -1, -1, R.string.title_program_type);
		titleBarHelper.setLeftMsg(R.string.left_back);
		lv = (ListView) findViewById(R.id.lv_ampc);
		lv.setOnItemClickListener(this);
		adapter = new MyProClassifyAdapter(this);
		lv.setAdapter(adapter);
		initNet();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent();
		intent.putExtra("typeId", list.get(position).getTypeId());
		intent.putExtra("type", list.get(position).getTypeName());
		setResult(RESULT_OK, intent);
		finish();
	}
	
	private void initNet(){
		showLoadingDialog("");
		FormResultRequest<ArrayList<ProTypeBean>> request = new FormResultRequest<ArrayList<ProTypeBean>>(UrlConstants.GETPROTYPE, initListener, this,
				new TypeToken<RequestResult<ArrayList<ProTypeBean>>>() {
				}.getType());
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putStringTypeParam("userId", GlobalApp.userModel.getUserId());
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}
	
	private Listener<RequestResult<ArrayList<ProTypeBean>>> initListener = new Listener<RequestResult<ArrayList<ProTypeBean>>>() {
		@Override
		public void onResponse(RequestResult<ArrayList<ProTypeBean>> arg0) {
			dismissLoadingDialog();
			list = arg0.getRs();
			adapter.setGroup(list);
		}
	};

}
